package com.leetroll.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leetroll.api.global.SysConf;
import com.leetroll.api.mapper.UserMapper;
import com.leetroll.api.service.SysConfigService;
import com.leetroll.api.service.UserService;
import com.leetroll.api.vo.LoginVO;
import com.leetroll.api.vo.UserVO;
import com.leetroll.common.constant.*;
import com.leetroll.common.entity.User;
import com.leetroll.common.entity.UserAction;
import com.leetroll.common.enums.EConfigKey;
import com.leetroll.common.enums.EStatus;
import com.leetroll.common.enums.EUserStatus;
import com.leetroll.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户表 服务实现类
 */
@Service
public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements UserService {

    private final SysConfigService sysConfigService;

    @Autowired
    public UserServiceImpl(SysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }

    @Override
    public IPage<User> getPageList(UserVO userVO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // 查询用户名
        queryWrapper.select(User.class, i -> !i.getProperty().equals(SQLConstant.PASS_WORD));
        Page<User> page = new Page<>();
        queryWrapper.ne(SQLConstant.STATUS, EStatus.DISABLED);
        IPage<User> pageList = this.page(page, queryWrapper);

        List<User> list = pageList.getRecords();

        final StringBuffer fileUids = new StringBuffer();
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getAvatar())) {
                fileUids.append(item.getAvatar()).append(SysConf.FILE_SEGMENTATION);
            }
        });
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    @Cacheable(cacheNames = "getUser", key = "#id")
    public User getUser(Long id) {
        return super.getById(id);
    }

    @Override
    public User addUser(LoginVO loginVO) {
        User user = new User();
        user.setName(loginVO.getName());
        user.setPassword(MD5Utils.string2MD5(loginVO.getPassword()));
        user.setPhone(loginVO.getPhone());
        user.setStatus(EUserStatus.ONLINE.getStatus());
        user.setVip(false);
        user.setAdmin(false);
        String bgLink = sysConfigService.listConfigFilesByKey(EConfigKey.DEFAULT_BG).get(new Random().nextInt(6));
        String avatarLink = sysConfigService.listConfigFilesByKey(EConfigKey.DEFAULT_AVATAR).get(new Random().nextInt(11));
        user.setAvatar(avatarLink);
        user.setBgImg(bgLink);
        user.insert();
        return user;
    }

    @Override
    public User addWeChatUser(String openid, String nickname, String headimgurl, int sex) {
        User user = new User();
        user.setName(nickname);
        user.setOpenid(openid);
        String bgLink = sysConfigService.getRandomOneDefaultBg();
        user.setBgImg(bgLink);
        user.setAvatar(headimgurl);
        user.setGender(sex);
        user.setStatus(EUserStatus.ONLINE.getStatus());
        user.insert();
        return user;
    }

    @Override
    public User getByOpenid(String openid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConstant.USER_OPENID, openid);
        return this.getOne(queryWrapper);
    }

    @Override
    public Boolean updateAvatar(UserVO userVO) {
        User user = this.getById(userVO.getId());
        user.setAvatar(userVO.getAvatar());
        user.setUpdateTime(new Date());
        return user.updateById();
    }

    @Override
    public Boolean updateBackImg(UserVO userVO) {
        User user = this.getById(userVO.getId());
        user.setBgImg(userVO.getBgImg());
        user.setUpdateTime(new Date());
        return user.updateById();
    }

    @Override
    public Boolean editUser(UserVO userVO) {
        User user = this.getById(userVO.getId());
        user.setName(userVO.getName());
        user.setGender(userVO.getGender());
        user.setMoto(userVO.getMoto());
        user.setUpdateTime(new Date());
        return user.updateById();
    }

    @Override
    public String deleteUser(UserVO userVO) {
        User user = this.getById(userVO.getId());
        user.setUpdateTime(new Date());
        user.updateById();
        return ResultUtil.successWithMessage(MessageConstant.DELETE_SUCCESS);
    }

    @Override
    public String resetUserPassword(UserVO userVO) {
//        String defaultPassword = sysParamsService.getSysParamsValueByKey(SysConf.SYS_DEFAULT_PASSWORD);
//        User user = userService.getById(userVO.getId());
//        user.setPassword(MD5Utils.string2MD5(defaultPassword));
//        user.setUpdateTime(new Date());
//        user.updateById();
//        return ResultUtil.successWithMessage(MessageConstant.OPERATION_SUCCESS);
        return null;
    }

    @Override
    public void updateWeChatUser(User user, String nickname, String headimgurl, int sex) {
        user.setName(nickname);
        user.setAvatar(headimgurl);
        user.setGender(sex);
        user.updateById();
    }
}
